package com.alan.wifishare.view.impl.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.alan.libapi.model.business.ConnectedDeviceInfo;
import com.alan.wifishare.R;
import com.alan.wifishare.presenter.CNTManageConnectedDevicesPS;
import com.alan.wifishare.view.BaseActivity;
import com.alan.wifishare.view.iview.IVCNTManageConnectedDevices;
import com.alan.wifishare.view.ui.CNTConnectedDeviceAdapter;
import com.alan.wifishare.view.ui.TwoBtnDialog;

import java.util.List;

/**
 * @className: CNTManageConnectedDevicesActivity
 * @describtion: CNTManageConnectedDevicesActivity
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/4/26 19:50
 */
public class CNTManageConnectedDevicesActivity extends BaseActivity implements IVCNTManageConnectedDevices, View.OnClickListener {

    private CNTManageConnectedDevicesPS cntManageConnectedDevicesPS;
    private Button cntManageConnectedDevicesBackBtn;
    private ListView cntManageConnectedDevicesListview;
    private CNTConnectedDeviceAdapter cntConnectedDeviceAdapter;
    private TwoBtnDialog twoBtnDialog;
    private ConnectedDeviceInfo willBeDealedDeviceInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cnt_manage_connected_device_layout);
        cntManageConnectedDevicesPS = new CNTManageConnectedDevicesPS(this);
        initView();
        cntManageConnectedDevicesPS.getConnectedDevices();
    }

    @Override
    protected void initView() {
        cntManageConnectedDevicesBackBtn = (Button)findViewById(R.id.cntManageConnectedDevicesBackBtn);
        cntManageConnectedDevicesBackBtn.setOnClickListener(this);
        cntManageConnectedDevicesListview = (ListView)findViewById(R.id.cntManageConnectedDevicesListview);
        cntConnectedDeviceAdapter = new CNTConnectedDeviceAdapter(this);
        cntManageConnectedDevicesListview.setAdapter(cntConnectedDeviceAdapter);
        cntManageConnectedDevicesListview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                willBeDealedDeviceInfo = cntConnectedDeviceAdapter.getItemList().get(i);
                initTwoBtnDialog();
                twoBtnDialog.show();
                return false;
            }
        });
    }

    @Override
    public void displayConnectedDevices(List<ConnectedDeviceInfo> devicesList) {
        cntConnectedDeviceAdapter.setItemList(devicesList);
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void toastMsg(String msg) {
        toastMsgShort(msg);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(twoBtnDialog != null && twoBtnDialog.isShowing()){
            twoBtnDialog.dismiss();
        }
    }

    private void initTwoBtnDialog(){
        if(twoBtnDialog == null){
            twoBtnDialog = new TwoBtnDialog(this, R.style.custom_dialog1);
            twoBtnDialog.setTitle("踢除设备提示");
            twoBtnDialog.setLeftBtn(getResources().getString(R.string.dialog_cancel_btn), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    twoBtnDialog.dismiss();
                }
            });
            twoBtnDialog.setRightBtn(getResources().getString(R.string.dialog_yes_btn), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    cntManageConnectedDevicesPS.moveOutConnectedDevice(willBeDealedDeviceInfo);
                    twoBtnDialog.dismiss();
                }
            });
        }
        twoBtnDialog.setMessage("确定要将" + willBeDealedDeviceInfo.getDeviceName() + "设备从Link上踢除其连接？");
    }

    @Override
    public void onClick(View view) {
        CNTManageConnectedDevicesActivity.this.finish();
    }
}
